/*
 * This file is part of Euphrat.
 *
 * Copyright © 2012 Bart Verhoeven
 *
 * Euphrat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Euphrat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Euphrat. If not, see <http://www.gnu.org/licenses/>.
 */
package org.euphrat.client.view.tab;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The core component of the tab framework. The actual content of a tab is
 * specified by {@link #createWidget()}. To perform additional steps when
 * displaying or hiding a tab, override {@link #onShow()} and {@link #onHide()}.
 *
 * @author Bart Verhoeven
 */
public abstract class Tab implements IsWidget {

  /** Tab name. */
  private final String name;
  /** Tab description. */
  private final String description;
  /** Native tab background color. */
  private final String backgroundColor;
  /** Actual tab widget. */
  private Widget component;

  /**
   * Creates a new tab.
   *
   * @param name name of the tab
   * @param description tab description
   * @param backgroundColor native tab background color
   */
  protected Tab(String name, String description, String backgroundColor) {
    this.name = name;
    this.description = description;
    this.backgroundColor = backgroundColor;
  }

  /**
   * Returns the name of this tab.
   *
   * @return the tab name
   */
  public String getName() {
    return name;
  }

  /**
   * Returns the description of this tab.
   *
   * @return the tab description
   */
  public String getDescription() {
    return description;
  }

  /**
   * Returns the background color of this tab.
   *
   * @return the native tab background color
   */
  public String getBackgroundColor() {
    return backgroundColor;
  }

  /**
   * Returns the actual widget of this tab. The widget itself is created on
   * demand, meaning it will only be initiated upon display in a user interface,
   * i.e. when invoking {@link Widget#setVisible(boolean) setVisible}.
   *
   * @return the tab widget
   */
  @Override
  public final Widget asWidget() {
    if (component == null) {
      Widget delegate = createWidget();
      component = new LazyWidget(delegate);
    }

    return component;
  }

  /**
   * To be invoked when displaying the tab. Does nothing by default.
   */
  public void onShow() {
    // NO-OP
  }

  /**
   * To be invoked when hiding the tab. Does nothing by default.
   */
  public void onHide() {
    // NO-OP
  }

  /**
   * Creates the actual widget of this tab.
   *
   * @return the actual tab widget
   */
  protected abstract Widget createWidget();

  /**
   * Basic implementation of a lazy widget.
   */
  private static class LazyWidget extends LazyPanel {
    /** Delegate widget. */
    private final Widget delegate;

    /**
     * Creates a lazy wrapper around a delegate widget.
     *
     * @param delegate the widget to wrap
     */
    public LazyWidget(Widget delegate) {
      this.delegate = delegate;
    }

    @Override
    protected Widget createWidget() {
      return delegate;
    }
  }
}
